<template>
  <div class="main-container">
    <el-card class="box-card !border-none" shadow="never">
      <div class="flex justify-between items-center">
        <span class="text-lg">{{ pageName }}</span>
      </div>
      <el-card class="box-card !border-none !px-[35px]" shadow="never">
        <el-row class="flex">
            <el-col :span="8" class="min-w-[100px]">
                <div class="statistic-card">
                    <el-statistic :value="pointsLogTable.increase_amount_sum" />
                    <div class="statistic-footer">
                        <div class="footer-item text-[14px] text-[#666]">
                          总增加积分
                        </div>
                    </div>
                </div>
            </el-col>
            <el-col :span="8" class="min-w-[100px]">
                <div class="statistic-card">
                    <el-statistic :value="pointsLogTable.reduce_amount_sum" />
                    <div class="statistic-footer">
                        <div class="footer-item text-[14px] text-[#666]">
                            总减少积分
                        </div>
                    </div>
                </div>
            </el-col>
        </el-row>
    </el-card>
      <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
        <el-form :inline="true" :model="pointsLogTable.searchParam" ref="searchFormRef">
          <el-form-item v-if="!pointsLogTable.searchParam?.member_id" :label="t('username')" prop="username">
            <el-input v-model="pointsLogTable.searchParam.username" placeholder="请输入" />
          </el-form-item>
          <el-form-item :label="t('pointsType')" prop="points_type">
            <el-select v-model="pointsLogTable.searchParam.points_type">
              <el-option
                v-for="(item, index) in pointTypeList"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="t('type')" prop="type">
            <el-select v-model="pointsLogTable.searchParam.type">
              <el-option
                v-for="(item, index) in pointChangeTypeList"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="t('createTime')" prop="create_time">
            <el-date-picker v-model="pointsLogTable.searchParam.create_time" type="datetimerange"
                value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="起始日期"
                end-placeholder="结束日期" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loadPointsLogList()">{{
              t("search")
            }}</el-button>
            <el-button @click="resetForm(searchFormRef)">{{ t("reset") }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="mt-[10px]">
        <el-table
          :data="pointsLogTable.data"
          size="large"
          v-loading="pointsLogTable.loading"
        >
          <template #empty>
            <span>{{ !pointsLogTable.loading ? t("emptyData") : "" }}</span>
          </template>
          <el-table-column
            type="index"
            :label="t('serialNumber')"
            min-width="60"
          />
          <el-table-column
            prop="username"
            :label="t('username')"
            min-width="80"
            show-overflow-tooltip
          />

          <el-table-column :label="t('pointsType')" min-width="80">
            <template #default="{ row }">
              <Dict-tag :value="row.points_type" :list="pointTypeList" />
            </template>
          </el-table-column>

          <el-table-column
            prop="amount"
            :label="t('amount')"
            min-width="80"
            show-overflow-tooltip
          />

          <el-table-column
            prop="before_amount"
            :label="t('beforeAmount')"
            min-width="80"
            show-overflow-tooltip
          />

          <el-table-column
            prop="after_amount"
            :label="t('afterAmount')"
            min-width="80"
            show-overflow-tooltip
          />
          <el-table-column :label="t('type')" min-width="80">
            <template #default="{ row }">
              <Dict-tag :value="row.type" :list="pointChangeTypeList" />
            </template>
          </el-table-column>
          <el-table-column
            prop="create_time"
            :label="t('createTime')"
            min-width="80"
            show-overflow-tooltip
          />
          <el-table-column :label="t('operation')" fixed="right" align="center" min-width="80">
            <template #default="{ row }">
              <el-button type="primary" link @click="deleteEvent(row.id)">{{
                t("delete")
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination
            v-model:current-page="pointsLogTable.page"
            v-model:page-size="pointsLogTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pointsLogTable.total"
            @size-change="loadPointsLogList()"
            @current-change="loadPointsLogList"
          />
        </div>
      </div>

    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue";
import { t } from "@/lang";
import { useDictionary } from "@/app/api/dict";
import { getPointsLogList, getPointsLogStatistics, deletePointsLog } from "@/app/api/distributor";
import { ElMessageBox, FormInstance } from "element-plus";
import { useRoute } from "vue-router";
const route = useRoute();
const pageName = route.meta.title;

const pointsLogTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  increase_amount_sum: 0, // 总增加积分
  reduce_amount_sum: 0, // 总减少积分
  searchParam: {
    member_id: null,
    username: null,
    points_type: null,
    type: null,
    create_time: null,
  },
});

const searchFormRef = ref<FormInstance>();
const pointTypeList = ref<any[]>([]);
const pointChangeTypeList = ref<any[]>([]);

onMounted(() => {
  if (route.query?.id) {
    pointsLogTable.searchParam.member_id = route.query.id
  }
  if (route.query?.type) {
    pointsLogTable.searchParam.points_type = route.query.type
  }
  useDictionary("point_type").then((res: any) => {
    pointTypeList.value = res.data.dictionary;
  });
  useDictionary("point_change_type").then((res: any) => {
    pointChangeTypeList.value = res.data.dictionary;
  });
  loadPointsLogList();
});

/**
 * 获取积分变动记录列表
 */
const loadPointsLogList = (page: number = 1) => {
  pointsLogTable.loading = true;
  pointsLogTable.page = page;

  getPointsLogList({
    page: pointsLogTable.page,
    limit: pointsLogTable.limit,
    ...pointsLogTable.searchParam,
  })
    .then((res) => {
      pointsLogTable.loading = false;
      pointsLogTable.data = res.data.data;
      pointsLogTable.total = res.data.total;
    })
    .catch(() => {
      pointsLogTable.loading = false;
    });

    getPointsLogStatistics({
      page: pointsLogTable.page,
      limit: pointsLogTable.limit,
      ...pointsLogTable.searchParam,
    }).then((res) => {
      pointsLogTable.increase_amount_sum = res.data.increase_amount_sum || 0;
      pointsLogTable.reduce_amount_sum = res.data.reduce_amount_sum || 0;
    })
};
/**
 * 删除积分变动记录
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t("pointsLogDeleteTips"), t("warning"), {
    confirmButtonText: t("confirm"),
    cancelButtonText: t("cancel"),
    type: "warning",
  }).then(() => {
    deletePointsLog(id)
      .then(() => {
        loadPointsLogList();
      })
      .catch(() => {});
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  loadPointsLogList();
};
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
